
/*
'===============================================================================
'  HNG.Portal.BL.TblLienHeManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace HNG.Portal.BLL
{
	public class TblLienHeManager : TblLienHeManagerBase
	{
        SqlConnection Conn = new SqlConnection(HNG.LIB.Common.Connection.strConn);
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblLienHeManager()
		{
			// Nothing for now.
		}

        public DataTable Get_Bykeysearch(string keysearch, string columnName)
        {           
            string strQuery = "select * from TblLienHe ";
            if (columnName != "")
                strQuery += " where " + columnName + " like N'%" + keysearch + "%'" + " Order by ID desc";
            else
                strQuery += " where TenNguoiGui like N'%" + keysearch + "%'" + " Order by ID desc";
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, strQuery);
        }

        public DataTable SelectCommon(TblLienHeEntity _TblLienHeEntity)
        {
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblLienHeEntity.SortExpression) ? "[ID]" : _TblLienHeEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblLienHeEntity.SortDirection) ? " DESC" : _TblLienHeEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[Id],").Append("\n");
                sbSql.Append("  ch.[LangID],").Append("\n");
                sbSql.Append("  ch.[TenNguoiGui],").Append("\n");
                sbSql.Append("  ch.[DienThoai],").Append("\n");
                sbSql.Append("  ch.[Email],").Append("\n");
                sbSql.Append("  ch.[GuiDen],").Append("\n");
                sbSql.Append("  ch.[TieuDe],").Append("\n");
                sbSql.Append("  ch.[MoTa],").Append("\n");
                sbSql.Append("  ch.[NgayGui],").Append("\n");
                sbSql.Append("  ch.[DanhDau],").Append("\n");
                sbSql.Append("  ch.[TrangThai],").Append("\n");
                sbSql.Append("  lng.[Name] LangName").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblLienHe ch").Append("\n");
                sbSql.AppendFormat("  LEFT JOIN tblLang lng").Append("\n");
                sbSql.Append("  ON lng.ID = ch.LangID").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblLienHeEntity.ID))
                {
                    sbSql.AppendFormat("  AND ch.[ID] LIKE {0}", _TblLienHeEntity.ID).Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblLienHeEntity.Condition))
                {
                    sbSql.AppendFormat("  AND (ch.[ID] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[LangID] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TenNguoiGui] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DienThoai] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Email] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[GuiDen] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TieuDe] LIKE N'%' + {0} + '%'", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE N'%' + {0} + '%')", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DanhDau] LIKE N'%' + {0} + '%')", _TblLienHeEntity.Condition).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TrangThai] LIKE N'%' + {0} + '%')", _TblLienHeEntity.Condition).Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblLienHeEntity.LangId))
                    sbSql.AppendFormat("  AND lng.[ID] like '{0}'", _TblLienHeEntity.LangId).Append("\n");

                if (!string.IsNullOrEmpty(_TblLienHeEntity.TrangThai.ToString()))
                    sbSql.AppendFormat("  AND ch.[TrangThai] like {0}", _TblLienHeEntity.TrangThai).Append("\n");

                if (!string.IsNullOrEmpty(_TblLienHeEntity.DanhDau.ToString()))
                    sbSql.AppendFormat("  AND ch.[DanhDau] like {0}", _TblLienHeEntity.DanhDau).Append("\n");

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblLienHeEntity.StartIndex, _TblLienHeEntity.EndIndex).Append("\n");
            }
            catch { }
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString());
        }

        public DataTable be_selectToGridView(TblLienHeEntity _TblLienHeEntity)
        {
            System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
            SqlParameter[] _SqlParameter = new SqlParameter[3];
            try
            {
                sbSql.Append("WITH tblTemp AS").Append("\n");
                sbSql.Append("(").Append("\n");
                sbSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY ");
                sbSql.AppendFormat("ch.{0} ", string.IsNullOrEmpty(_TblLienHeEntity.SortExpression) ? "[ID]" : _TblLienHeEntity.SortExpression);
                sbSql.Append(string.IsNullOrEmpty(_TblLienHeEntity.SortDirection) ? " DESC" : _TblLienHeEntity.SortDirection);
                sbSql.Append(") AS RowNum,").Append("\n");
                sbSql.Append("  ch.[Id],").Append("\n");
                sbSql.Append("  ch.[TenNguoiGui],").Append("\n");
                sbSql.Append("  ch.[DienThoai],").Append("\n");
                sbSql.Append("  ch.[Email],").Append("\n");
                sbSql.Append("  ch.[TieuDe],").Append("\n");
                sbSql.Append("  ch.[DanhDau]").Append("\n");
                sbSql.Append("  FROM").Append("\n");
                sbSql.Append("  tblLienHe ch").Append("\n");
                sbSql.Append("  WHERE").Append("\n");
                sbSql.Append("  1 = 1").Append("\n");

                if (!string.IsNullOrEmpty(_TblLienHeEntity.ID))
                {
                    _SqlParameter[0] = new SqlParameter("@ID", _TblLienHeEntity.ID);
                    sbSql.Append("  AND ch.[ID] LIKE @ID").Append("\n");

                }
                string strCon = _TblLienHeEntity.Condition;
                if (!string.IsNullOrEmpty(_TblLienHeEntity.Condition))
                {
                    sbSql.AppendFormat("  AND (ch.[TenNguoiGui] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DienThoai] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Email] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[GuiDen] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TieuDe] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE '%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[NgayGui] LIKE '%{0}%'", strCon).Append("\n");

                    sbSql.AppendFormat("  OR ch.[TenNguoiGui] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[DienThoai] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[Email] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[GuiDen] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[TieuDe] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[MoTa] LIKE N'%{0}%'", strCon).Append("\n");
                    sbSql.AppendFormat("  OR ch.[NgayGui] LIKE N'%{0}%'", strCon).Append("\n");

                    sbSql.Append(")").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblLienHeEntity.LangId))
                {
                    _SqlParameter[1] = new SqlParameter("@LangId", _TblLienHeEntity.LangId);
                    sbSql.Append("  AND ch.[LangId] LIKE @LangId").Append("\n");
                }

                if (!string.IsNullOrEmpty(_TblLienHeEntity.Status))
                {
                    _SqlParameter[2] = new SqlParameter("@TrangThai", _TblLienHeEntity.TrangThai);
                    sbSql.Append("  AND ch.[TrangThai] LIKE @TrangThai").Append("\n");
                }

                sbSql.Append(")").Append("\n");
                sbSql.Append("SELECT *, (SELECT Max(RowNum) FROM tblTemp) TotalItems FROM tblTemp").Append("\n");
                sbSql.AppendFormat("WHERE RowNum BETWEEN {0} AND {1}", _TblLienHeEntity.StartIndex, _TblLienHeEntity.EndIndex).Append("\n");
            }
            catch { }
            return AE.SqlHelper.ExecuteDataTable(Conn, CommandType.Text, sbSql.ToString(), _SqlParameter);
        }
	}
}
